package com.fyzk.activity.user.accountdetail;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.fyzk.bean.AccountDetailsBean;
import com.tyjc.lottery.R;
import com.fyzk.net.PublicMethod;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import static com.tyjc.lottery.R.id.withdraw_account;

/**
 * 账户明细
 */
public class AccountDetailListAdapter extends BaseAdapter {

    protected Context context;
    protected List<AccountDetailsBean> list = new ArrayList<>();

    public AccountDetailListAdapter(Context context, List<AccountDetailsBean> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.size();
    }

    @Override
    public Object getItem(int arg0) {

        return list.get(arg0);
    }

    @Override
    public long getItemId(int arg0) {

        return arg0;
    }

    public View getView(int arg0, View arg1, ViewGroup arg2) {

        AccountDetailsBean infor = (AccountDetailsBean) list.get(arg0);
        if (infor.getStatusMemo() == null) {
            arg1 = LayoutInflater.from(context).inflate(R.layout.zhanghumx_list, null);
            ViewHolder holder = new ViewHolder();
            holder.title = (TextView) arg1.findViewById(R.id.zhhmx_list_title);
            holder.time1 = (TextView) arg1.findViewById(R.id.zhhmx_list_time1);
            holder.zhhmx_tag = (ImageView) arg1.findViewById(R.id.zhhmx_tag);
            holder.money = (TextView) arg1.findViewById(R.id.zhhmx_list_money);
            holder.balance = (TextView) arg1.findViewById(R.id.zhhmx_list_balance);

            holder.title.setText(infor.getDescription());
            SimpleDateFormat dateFormat = new SimpleDateFormat("MM-dd HH:mm:ss");
//            SimpleDateFormat dateFormat1 = new SimpleDateFormat("MM-dd");
//            holder.time1.setText(infor.getTimestamp()+"");
            holder.time1.setText(dateFormat.format(new Date(infor.getTimestamp())));

            String tag = "";
            if (infor.getSignStatus() == 1) {
                tag = "+";
                holder.zhhmx_tag.setImageDrawable(context.getResources().getDrawable(R.drawable
                        .account_add));
            } else {
                tag = "-";
                holder.zhhmx_tag.setImageDrawable(context.getResources().getDrawable(R.drawable
                        .account_sub));
            }
            holder.balance.setText("彩金：" + PublicMethod.formatMoney(infor.getBalance()
                    .doubleValue()));

            holder.money.setText(tag + PublicMethod.formatMoney(infor.getAmount().doubleValue()));
            arg1.setTag(holder);
        }else{
            arg1 = LayoutInflater.from(context).inflate(R.layout.item_withdrawrecord, null);
            ViewHolderWithDraw holder = new ViewHolderWithDraw();
            holder.withdraw_amount = (TextView) arg1.findViewById(R.id.withdraw_amount);
            holder.time = (TextView) arg1.findViewById(R.id.zhhmx_list_time);
            holder.time1 = (TextView) arg1.findViewById(R.id.zhhmx_list_time1);
            holder.withdraw_account = (TextView) arg1.findViewById(withdraw_account);
            holder.withdraw_state = (TextView) arg1.findViewById(R.id.withdraw_state);

            SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss");
            SimpleDateFormat dateFormat1 = new SimpleDateFormat("MM-dd");
            try {
                long createTime = Long.parseLong(infor.getCreateTime());

                holder.time.setText(dateFormat.format(new Date(createTime)));
                holder.time1.setText(dateFormat1.format(new Date(createTime)));
            } catch (Exception e) {

            }

            int withdrawAmount = Integer.parseInt(infor.getAmount().longValue()+"") / 100;

            holder.withdraw_amount.setText(withdrawAmount+"");
            arg1.setTag(R.string.app_name,infor);

            arg1.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(context,WithDrawRecordDetailActivity.class);
                    AccountDetailsBean infor = (AccountDetailsBean) v.getTag(R.string.app_name);
                    intent.putExtra(WithDrawRecordDetailActivity.ID,infor.getId());
                    if(infor.getDrawType() == 1){//银行卡提现
                        intent.putExtra(WithDrawRecordDetailActivity.TYPE,"银行卡提现");
                        intent.putExtra(WithDrawRecordDetailActivity.ACCOUNT,infor.getBankName()+infor.getBankId());
                    }else if(infor.getDrawType() == 2){//支付宝提现
                        intent.putExtra(WithDrawRecordDetailActivity.TYPE,"支付宝提现");
                        intent.putExtra(WithDrawRecordDetailActivity.ACCOUNT,infor.getBankId());
                    }
                    intent.putExtra(WithDrawRecordDetailActivity.STARTTIME,infor.getCreateTime());
                    intent.putExtra(WithDrawRecordDetailActivity.FINISHTIME,infor.getFinishTime());
                    intent.putExtra(WithDrawRecordDetailActivity.AMOUNT,infor.getAmount().longValue()+"");
                    intent.putExtra(WithDrawRecordDetailActivity.DESCRIPTION,infor.getDescription());
                    intent.putExtra(WithDrawRecordDetailActivity.STATUS,infor.getStatusMemo());
                    intent.putExtra(WithDrawRecordDetailActivity.STATUSVALUE,infor.getStatus());
                    context.startActivity(intent);
                }
            });
            holder.withdraw_state.setText("提现("+infor.getStatusMemo()+")");
            String tag = "";
            arg1.setTag(holder);
        }

        return arg1;
    }


    class ViewHolderWithDraw {
        public TextView time1;
        TextView time;
        TextView withdraw_amount;
        TextView withdraw_account;
        TextView withdraw_state;
    }

    class ViewHolder {
        TextView title;
        TextView time;
        TextView money;
        ImageView zhhmx_tag;
        TextView balance;
        public TextView time1;
    }

    public void setList(List<AccountDetailsBean> list) {
        this.list = list;
        this.notifyDataSetChanged();
    }

    public List<AccountDetailsBean> getList() {
        return this.list;
    }

}
